<?php
class Posts extends Model {
	function Posts(){
		parent::Model();
    }
    
    function insertPost($user_id=NULL,
    					$group_id=NULL,
    					$message=NULL,
    					$mentioned=NULL,
    					$attached=NULL,
    					$posttags=NULL,
    					$comments=NULL,
    					$date=NULL,
    					$date_lastcomment=NULL){
   		$query = "INSERT INTO posts (user_id,
	    							group_id,
			    					message,
			    					mentioned,
			    					attached,
			    					posttags,
			    					comments,
			    					date) VALUES ('$user_id',
									    					'$group_id',
									    					'$message',
									    					'$mentioned',
									    					'$attached',
									    					'$posttags',
									    					'$comments',
									    					'$date')";
    	if($query = $this->db->query($query)){
    		return $this->db->insert_id();
    	}
    	return FALSE;
    }
    
    function getMyPost($user_id,$offset=0,$limit=10){
    	$return = FALSE;
    	$query = "SELECT * FROM posts WHERE user_id='$user_id' ORDER BY id DESC LIMIT $limit OFFSET $offset";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
   	 		foreach ($query->result() as $row){
   	 			$return['post_id'][]  = $row->id;
				$return['user_id'][]  = $row->user_id;
				$return['message'][] = $row->message;
				$return['date'][] = $row->date;
				$return['comments'][]=$row->comments;
			}
    	}
    	return $return;
    }
    
	function getPost($post_id){
    	$return = FALSE;
    	$query = "SELECT * FROM posts WHERE id='$post_id' LIMIT 1";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
   	 		foreach ($query->result() as $row){
   	 			$return['post_id']  = $row->id;
				$return['user_id']  = $row->user_id;
				$return['message'] = $row->message;
				$return['date'] = $row->date;
				$return['comments']=$row->comments;
				$return['attached'] = $row->attached;
			}
    	}
    	return $return;
    }
    
    
    function getAllPosts($offset=0,$limit=10){
    	$return = FALSE;
    	$query = "SELECT * FROM posts ORDER BY id DESC LIMIT $limit OFFSET $offset";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
   	 		foreach ($query->result() as $row){
   	 			$return['post_id'][]  = $row->id;
				$return['user_id'][]  = $row->user_id;
				$return['message'][] = $row->message;
				$return['date'][] = $row->date;
				$return['comments'][]=$row->comments;
			}
    	}
    	return $return;
    }
    function getFriendPosts ($user_id,$offset=0,$limit=10){
    	$return = FALSE;
    	$friend_id= array();
    	$query = "SELECT * FROM users_friend WHERE user_id='$user_id'";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
   	 		foreach ($query->result() as $row){
   	 			$friend_id [] = $row->friend_id;
   	 		}
   	 		
   	 		for($i=0;$i<sizeof($friend_id);$i++){
   	 			$query = "SELECT * FROM posts WHERE user_id = '$friend_id[$i]' ORDER BY id DESC";
    			$query = $this->db->query($query);
    			if($query->num_rows() > 0){
	    			foreach ($query->result() as $row){
	   	 				$return['post_idTemp'][]  = $row->id;
						
	   	 			}
    			}
   	 		}
   	 		sort($return['post_idTemp']);
    		for($j=sizeof($return['post_idTemp'])-1;$j>=0;$j--){
    			$query = "SELECT * FROM posts WHERE id='".$return['post_idTemp'][$j]."'";
    			$query = $this->db->query($query);
    			if($query->num_rows() > 0){
	    			foreach ($query->result() as $row){
	   	 				$post_id [] = $row->id;
		    			$user_idPost [] = $row->user_id;
						$message[] = $row->message;
						$date[] = $row->date;
						$comments[]=$row->comments;
	   	 			}
    			}
    		}	
	   	 	$return['post_id'] = array_slice($post_id, $offset, $limit); 
	   	 	$return['user_id'] = array_slice($user_idPost, $offset, $limit);
	   	 	$return['message'] = array_slice($message, $offset, $limit);
	   	 	$return['date'] = array_slice($date, $offset, $limit);
	   	 	$return['comments'] = array_slice($comments, $offset, $limit);	   	 			
   	 	}
    	return $return;
    }
 	function getMyLastPost($user_id){
    	$return = FALSE;
    	$query = "SELECT * FROM posts WHERE user_id='$user_id' ORDER BY id DESC LIMIT 1";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
   	 		foreach ($query->result() as $row){
   	 			$return['post_id'][]  = $row->id;
				$return['user_id'][]  = $row->user_id;
				$return['message'][] = $row->message;
				$return['date'][] = $row->date;
				$return['comments'][]=$row->comments;
			}
    	}
    	return $return;
    }
    function insertMentioned($post_id=NULL,$user_id=NULL){
   		$query = "INSERT INTO posts_mentioned (post_id,user_id) VALUES ('$post_id','$user_id')";
   		if($query = $this->db->query($query)){
    		return $this->db->insert_id();
    	}
    	return FALSE;
    }
    
	function insertTagged($post_id=NULL,$tag=NULL){
   		$query = "INSERT INTO posts_tagged (post_id,tag) VALUES ('$post_id','$tag')";
   		if($query = $this->db->query($query)){
    		return $this->db->insert_id();
    	}
    	return FALSE;
    }
    
    function searchPostsTagged($searchTag=NULL){
    	$return = FALSE;
    	$query = "SELECT * FROM posts_tagged WHERE UPPER (tag) = UPPER ('$searchTag') ORDER BY id DESC";
    	$query = $this->db->query($query);
    	if($query->num_rows() > 0){
    		$post_id = null;
   	 		foreach ($query->result() as $row){
   	 			if($post_id!=$row->post_id){
   	 				$post_id  = $row->post_id;
	   	 			$querySelect = "SELECT * FROM posts WHERE id='$post_id' AND posttags>0 LIMIT 1";
	   	 			$querySelect = $this->db->query($querySelect);
					foreach ($querySelect->result() as $rowSelect){
						$return['post_id'][]  = $rowSelect->id;
						$return['user_id'][]  = $rowSelect->user_id;
						$return['message'][] = $rowSelect->message;
						$return['date'][] = $rowSelect->date;
						$return['comments'][]=$rowSelect->comments;
					}
   	 			}
			}
			return $return;
    	}
    	return FALSE;
    }
    
    function addComment($post_id=NULL){
    	$numCumment = 0;
    	$query = "SELECT * FROM posts WHERE id='$post_id' LIMIT 1";
    	$query = $this->db->query($query);
    	foreach ($query->result() as $row){
			$numCumment =$row->comments;
		}
		$query = "UPDATE posts SET comments='".($numCumment+1)."' WHERE id='$post_id' ";
   		return $this->db->query($query);
    }
    function getPostGroup($group_id,$offset=0,$limit=10){
    	$return = FALSE;
    	$friend_id= array();
    	$query = "SELECT * FROM posts_mentioned WHERE group_id='$group_id'";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
   	 		foreach ($query->result() as $row){
   	 			$postGroup_id [] = $row->post_id;
   	 		}
   	 		sort($postGroup_id);
    		for($j=sizeof($postGroup_id)-1;$j>=0;$j--){
    			$query = "SELECT * FROM posts WHERE id='".$postGroup_id[$j]."'";
    			$query = $this->db->query($query);
    			if($query->num_rows() > 0){
	    			foreach ($query->result() as $row){
	   	 				$post_id [] = $row->id;
		    			$user_idPost [] = $row->user_id;
						$message[] = $row->message;
						$date[] = $row->date;
						$comments[]=$row->comments;
	   	 		